package pl.pkolodziej.platform.dao.course;

import java.sql.Timestamp;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import pl.pkolodziej.platform.vo.course.UserCourseInfo;

@Repository
public class UserCourseDAOImpl implements UserCourseDAO {

	@Autowired
	SessionFactory sessionFactory;
	
	public void updateUserCourse(String username,int course_id, Timestamp lastattempt,int pages, int score, int starttest, int endtest) {
		Query hql = sessionFactory.getCurrentSession().createQuery("update UserCourseInfo set lastattempt=:_lastattempt" +
				" ,score = :_score" +
				" ,starttest = :_starttest" +
				" ,endtest = :_endtest" +
				" ,pages = :_pages" +
				" where username= :_username and course_id=:_course_id");
		
		hql.setParameter("_lastattempt", lastattempt);
		hql.setParameter("_score", score);
		hql.setParameter("_starttest", starttest);
		hql.setParameter("_endtest", endtest);
		hql.setParameter("_pages", pages);
		hql.setParameter("_username", username);
		hql.setParameter("_course_id", course_id);

		
		hql.executeUpdate();
	}
	
	public void updateUserCourseLastattempt(String username,int course_id, Timestamp lastattempt) {
		Query hql = sessionFactory.getCurrentSession().createQuery("update UserCourseInfo set lastattempt=:_lastattempt" +
				" where username= :_username and course_id=:_course_id");
		
		hql.setParameter("_lastattempt", lastattempt);
		hql.setParameter("_username", username);
		hql.setParameter("_course_id", course_id);

		hql.executeUpdate();
	}
	
	public void updateUserCourseScoreAndPage(String username, int course_id,int pages, int score) {
		Query hql = sessionFactory.getCurrentSession().createQuery("update UserCourseInfo set" +
				" score = :_score" +
				" ,pages = :_pages" +
				" where username= :_username and course_id=:_course_id");

		hql.setParameter("_score", score);
		hql.setParameter("_pages", pages);
		hql.setParameter("_username", username);
		hql.setParameter("_course_id", course_id);

		hql.executeUpdate();
	}
	
	public void updateUserCourseStartTest(String username, int course_id,int starttest) {
		Query hql = sessionFactory.getCurrentSession().createQuery("update UserCourseInfo set" +
				" starttest = :_starttest" +
				" where username= :_username and course_id=:_course_id");
		
		hql.setParameter("_starttest", starttest);
		hql.setParameter("_username", username);
		hql.setParameter("_course_id", course_id);

		hql.executeUpdate();
	}
	
	public void updateUserCourseEndTest(String username, int course_id, int endtest) {
		Query hql = sessionFactory.getCurrentSession().createQuery("update UserCourseInfo set" +
				" endtest = :_endtest" +
				" where username= :_username and course_id=:_course_id");
		
		hql.setParameter("_endtest", endtest);
		hql.setParameter("_username", username);
		hql.setParameter("_course_id", course_id);

		hql.executeUpdate();
	}
	
	public void addUserCourse(UserCourseInfo uci) {
		sessionFactory.getCurrentSession().save(uci);
	}
}
